package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Alteration;
import visiopuce.objets.SiteFragile;

public class SiteFragileDAOImpl extends GenericDAOImpl<SiteFragile, BigDecimal> implements SiteFragileDAO {

	/** Constructeur priv� */
	private SiteFragileDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static SiteFragileDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static SiteFragileDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new SiteFragileDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<SiteFragile> getSiteFragileByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<SiteFragile> genes = null;
		String sql = null;
		Query query;
		sql = "SELECT s FROM SiteFragile s, Bande b WHERE b.libelle = s.bande and b.chromosome = :chromosome and s.chromosome = b.chromosome and b.debutHg19 IS NOT NULL AND b.debutHg19 <= :fin AND b.finHg19 IS NOT NULL AND b.finHg19 >= :debut ORDER BY b.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		genes = (ArrayList<SiteFragile>) query.list();
		return genes;
	}
}